package com.exchange.websocket.common.huobi.impl;


import com.exchange.websocket.common.huobi.WebSocketService;
import com.exchange.websocket.common.utils.redis.RedisUtil;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 订阅信息处理类需要实现WebSocketService接口
 *
 * @author okcoin
 */

public class HuobiBuissnesWebSocketServiceImpl implements WebSocketService {
    public static final Logger log = LoggerFactory.getLogger(HuobiBuissnesWebSocketServiceImpl.class);
    @Override
    public void onReceive(String msg,RedisUtil redisUtil) {
        log.info("HuobiWebSocket Client received message: " + msg);
        //string转map
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        Map<String, String> tickMap = new HashMap<>();
        map = gson.fromJson(msg, map.getClass());
        if (Objects.nonNull(map.get("tick"))) {
            Map<String, Object> map1 = ((Map) map.get("tick"));
            List bids = (List) map1.get("bids");
            List asks = (List) map1.get("asks");
            List bid = (List) bids.get(0);
            Double bidPrice = (Double) bid.get(0);
            Double bidAmount = (Double) bid.get(1);
            List ask = (List) asks.get(0);
            Double askPrice = (Double) ask.get(0);
            Double askAmount = (Double) ask.get(1);
            try {
                redisUtil.set("huobiBidPrice", bidPrice.toString());
                redisUtil.set("huobiBidAmount", bidAmount.toString());
                redisUtil.set("huobiAskPrice", askPrice.toString());
                redisUtil.set("huobiAskAmount", askAmount.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("HuobiWebSocket bidPrice=={},bidAmount=={},askPrice=={},askAmount=={}", bidPrice, bidAmount, askPrice, askAmount);
            log.info("success");
        }
        if(Objects.nonNull(map.get("ping"))){
            redisUtil.set("huobiPong",map.get("ping"));
        }

    }
}
